<template>
    <van-dialog :show="user.realShow" title="实名认证" :showConfirmButton="false" show-cancel-button
        @cancel="user.setRealShow(false)">
        <van-form @submit="executeAuth">
            <van-cell-group inset>
                <!-- 通过 pattern 进行正则校验 -->
                <van-field label="真实姓名" required v-model="state.name" name="name" placeholder="请输入真实姓名"
                    :rules="[{ pattern: state.checkName, message: '请输入正确格式姓名' }]" />
                <van-field label="身份证号" required v-model="state.idNo" name="idNo" placeholder="请输入身份证号"
                    :rules="[{ pattern: state.checkidNo, message: '请输入正确身份证号' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>
    </van-dialog>
</template>

<script setup lang='ts'>
import { reactive } from 'vue';
import { userStore } from '@/store';
import { realNameAuthentication } from '@/api';
import routes from '@/router/index';
const user = userStore()

const state = reactive({
    name: '',
    idNo: '',
    checkName: /^(?:[\u4e00-\u9fa5·]{2,16})$/,
    checkidNo: /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/
})

const executeAuth = (e: any) => {
    realNameAuthentication({ ...e, user_id: user.userInfo.user_id }).then((res: any) => {
        if (res.code == 200) {
            user.changeUserInfo(e)
            user.setpostsType('pet')
            user.setRealShow(false)
            routes.push({ name: 'publish' })
            user.setShowOverlay(false)
        }
    })

}
</script>

<style lang='less' scoped></style>
